require 'test/unit'
require 'lib/server.rb'
require 'timeout'

require 'rubygems'
require 'net/dns/mdns-sd'

class TestJobHandler < Test::Unit::TestCase
  def setup()
  end
  
  def teardown()
  end
end

class TestDatasetReceiver < Test::Unit::TestCase
  def setup()
  end
  
  def teardown()
  end
end

class TestDataResultMarshaller < Test::Unit::TestCase
  def setup()
  end
  
  def teardown()
  end
end

class TestDataProcessMarshaller < Test::Unit::TestCase
  def setup()
  end
  
  def teardown()
  end
end

class TestServerAnnouncer < Test::Unit::TestCase
  
  def setup()
    @announcer = DistributedWorker::Server::Announcer.new()
  end
  
  def teardown()
    @announcer = nil
  end
  
  def test_announcer()
    task_name = @announcer.announce('test-task')
    assert(is_broadcast?(task_name), 'Task was not announced')
    @announcer.revoke(task_name)
    assert(!is_broadcast?(task_name), 'Task was not revoked')
  end
    
  private 
  
  # return true if a task is currently being announced via mDNS
  # TODO: this works, but result isn't getting set
  def is_broadcast?(task = 'test-task')
    result = false
    t = Timeout::timeout(5) {
      query = Net::DNS::MDNSSD.browse('_foo._tcp') do |reply|
        if reply.name == task
          query.stop()
          result = true
        end
      end
      sleep(4)
      query.stop()
    }
    return result
  end
  
end

